package com.bwie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.pojo.TypeTemplate;
import com.bwie.service.BrandService;
import com.bwie.service.SpecificationService;
import com.bwie.service.TypeTemplateService;
import com.bwie.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : LeiMingkun
 * @date : 2024-08-21 08:06
 * @description : TypeTemplateController
 **/
@RestController
@RequestMapping("/manage/typeTemplate")
public class TypeTemplateController {

    @Resource
    private TypeTemplateService typeTemplateService;
    @Resource
    private BrandService brandService;
    @Resource
    private SpecificationService specificationService;

    @RequestMapping("/list")
    public R list(@RequestBody TypeTemplate typeTemplate){
        LambdaQueryWrapper<TypeTemplate> queryWarrper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(typeTemplate.getName())){
            queryWarrper.like(TypeTemplate::getName,typeTemplate.getName());
        }
        Page<TypeTemplate> pagedb = new Page<>(typeTemplate.getStartPage(),typeTemplate.getEndPage());
        Page<TypeTemplate> pageResult = this.typeTemplateService.page(pagedb, queryWarrper);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("list",pageResult.getRecords());
        map.put("total",pageResult.getTotal());
        return R.success(map);
    }

    @RequestMapping("/listAll")
    public R listAll(){
        return R.success(this.typeTemplateService.list());
    }

    @RequestMapping("/getBrandList")
    public R getBrandList(){
        return R.success(this.brandService.list());
    }

    @RequestMapping("/getSpecIdsList")
    public R getSpecIdsList(){
        return R.success(this.specificationService.list());
    }

    @RequestMapping("/save")
    public R save(@RequestBody TypeTemplate typeTemplate){
        return R.success(this.typeTemplateService.save(typeTemplate));
    }

    @RequestMapping("/delById")
    public R delById(@RequestParam(name = "id")Integer id){
        return R.success(typeTemplateService.removeById(id));
    }

    @RequestMapping("/updateById")
    public R updateById(@RequestBody TypeTemplate typeTemplate){
        return R.success(typeTemplateService.updateById(typeTemplate));
    }
}